package com.leyou.listener;


import com.aliyuncs.exceptions.ClientException;
import com.leyou.util.DxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class Listener {

    @Autowired
    private DxUtil dxUtil;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "dx.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.dx.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = "dx.code"
    ))
    public void listenCreate(Map<String ,String> map){
        String telphone = map.get("telphone");
        if(telphone==null || telphone.equals("")){
            return;
        }
        String template = "{\"code\":\"" + map.get("code")+"\"}";
        try {
            dxUtil.contextLoads(template,telphone);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}
